﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
           /* string cmdString = "server=.;database=Student ;uid=sa;pwd=123456";

            SqlConnection sqlConnection = new SqlConnection();

            SqlDataAdapter dataAdapter = new SqlDataAdapter(cmdString );*/

            string cmdString = "select * from Student";

            var dt = DbHelper.GetDataTable(cmdString);

            dataGridView1.DataSource = dt;

            dataGridView1.ReadOnly = true;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //选择一整行

            dataGridView1.AllowUserToAddRows = false;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;

            var sql = string.Format("selset * from Student where StudentNmae like '%{0}%',Name");

            var dt = DbHelper.GetDataTable(sql);

            dataGridView1.DataSource = dt;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Mysolo mysolo = new Mysolo();

            var res = mysolo.ShowDialog();

            if (res == DialogResult.Yes)
            {
                var name = textBox1.Text;

                var cmdString = string .Format ("delete * from Student where like'%{0}%'",name);

                var dt = DbHelper.GetDataTable(cmdString);

                dataGridView1.DataSource = dt;


                MessageBox.Show("Yes");
            }
            else
            {
                MessageBox.Show("No");
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {   //有改动
            if(dataGridView1 .SelectedRows .Count > 0)// Name,Salary,ManagerId
            {
                var Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;

                var Name =(string ) dataGridView1.SelectedRows[0].Cells["Id"].Value;

                var Salary = (int) dataGridView1.SelectedRows[0].Cells["Salary"].Value;

                var ManagerId = (int)dataGridView1.SelectedRows[0].Cells["ManagerId"].Value;

                Mysolo mysolo = new Mysolo(Id,Name, Salary, ManagerId);

                var res = mysolo.ShowDialog();

                if (res == DialogResult.Yes)
                {
                    var SName = textBox1.Text;

                    var cmdString = string.Format("delete * from Student where like'%{0}%'", Name);

                    var dt = DbHelper.GetDataTable(cmdString);

                    dataGridView1.DataSource = dt;


                    MessageBox.Show("Yes");
                }
                else
                {
                    MessageBox.Show("No");
                }
            }
            else
            {
                MessageBox.Show("未选择数据行", "提示！");
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if(dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];

                var sno = (int)row.Cells["Id"].Value;

                var res = MessageBox.Show("确定要删除！", "疯狂提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete * from Student where like'%{0}%'", sno);

                    var rowCount = DbHelper.AddOrEditSave(sql);

                    if (rowCount > 0)
                    {
                        var name = textBox1.Text;

                        string cmdString = "select * from Student";

                        var dt = DbHelper.GetDataTable(cmdString);

                        dataGridView1.DataSource = dt;

                        MessageBox.Show("删除成功", "提示");
                    }
                }
                else
                {
                 /*   string cmdString = "select * from Student";

                    var dt = DbHelper.GetDataTable(cmdString);

                    dataGridView1.DataSource = dt;*/

                    MessageBox.Show("未选择行", "提示！");
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
